package org.jarx.android.reader;

import java.io.File;
import java.io.IOException;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TextView;
import android.util.Log;

public class HomeActivity extends TabActivity {

    private static final String TAG = "HomeActivity";

    private ServiceConnection serviceConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
        }
        @Override
        public void onServiceDisconnected(ComponentName className) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        checkErrorReport();

        bindService(new Intent(this, ReaderService.class),
            this.serviceConn, Context.BIND_AUTO_CREATE);

        TabHost tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("subList")
                .setIndicator("sub list")
                .setContent(new Intent(this, SubListActivity.class)));
        tabHost.addTab(tabHost.newTabSpec("tagList")
                .setIndicator("tag list")
                .setContent(new Intent(this, TagListActivity.class)));

        ActivityHelper.bindTitle(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(this.serviceConn);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onRestoreInstanceState(Bundle state) {
        TabHost tabHost = getTabHost();
        String cur = state.getString("currentTab");
        if (cur != null) {
            tabHost.setCurrentTabByTag(cur);
        }
        // NOTE: for SDK bugs, don't calll super method
        // super.onRestoreInstanceState(state);
    }

    private void checkErrorReport() {
        final Context c = getApplicationContext();
        final File errReportFile = Utils.getErrorReportFile(this);
        if (errReportFile.exists()) {
            errReportFile.delete();
        }
        File errFile = Utils.getErrorFile(this);
        if (errFile.exists()
                && errFile.renameTo(errReportFile)
                && Prefs.isEnableErrReporting(c)) {
            new AlertDialog.Builder(this)
                .setTitle(R.string.txt_err_report)
                .setMessage(R.string.msg_send_err_report)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        progressSendErrorReport(errReportFile);
                    }
                })
                .setNeutralButton("Disable", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        Prefs.setEnableErrReporting(c, false);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                }).show();
        }
    }

    private void progressSendErrorReport(final File file) {
        final Handler handler = new Handler();
        final Context c = getApplicationContext();
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setIndeterminate(true);
        dialog.setMessage(getText(R.string.msg_sending_err_report));
        dialog.show();
        new Thread() {
            public void run() {
                try {
                    Utils.sendErrorReport(file);
                } catch (IOException e) {
                    ActivityHelper.showToast(c, e);
                }
                handler.post(new Runnable() {
                    public void run() {
                        dialog.dismiss();
                    }
                });
            }
        }.start();
    }
}
